﻿namespace JunFan.Domain
{
    using System;

    public class Item
    {
        public int InternalId { get; set; }

        public string Id { get; set; }

        public string Description { get; set; }

        public DateTime CreatedOn { get; set; }

        public DateTime? StartedOn { get; set; }

        public DateTime? CompletedOn { get; set; }

        public int LeadTime
        {
            get
            {
                DateTime upTo = IsCompleted()
                                    ? CompletedOn.Value
                                    : DateTime.Today;

                return (upTo - CreatedOn).Days;
            }
        }

        public int CycleTime
        {
            get
            {
                switch (ItemStatus)
                {
                    case ItemStatus.Completed:
                        return (CompletedOn.Value - StartedOn.Value).Days;

                    case ItemStatus.InProgress:
                        return (DateTime.Today - StartedOn.Value).Days;

                    case ItemStatus.Created:
                        return 0;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }

        protected ItemStatus ItemStatus
        {
            get { 
            
                if (CompletedOn.HasValue)
                {
                    return ItemStatus.Completed;
                }
                
                if (StartedOn.HasValue)
                {
                    return ItemStatus.InProgress;    
                }

                return ItemStatus.Created;
            }
        }

        public bool IsCompleted()
        {
            return ItemStatus == ItemStatus.Completed;
        }

        public bool IsStarted()
        {
            return StartedOn.HasValue;
        }

        public bool IsInProgress()
        {
            return ItemStatus == ItemStatus.InProgress;
        }

        public bool IsInBacklog()
        {
            return ItemStatus == ItemStatus.Created;
        }
    }
}